package com.prdiot.poetry.dao;

import com.prdiot.poetry.domains.Mingju;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface MingjuRepository extends JpaRepository<Mingju, Integer> {

    @Query("select m from Mingju m where m.body like %?1%")
    Page<Mingju> searchByKey(@Param("key") String key, Pageable pageable);

    @Query(value = "select distinct theme from mingju", nativeQuery=true)
    List<String> getMingjuTheme();

    @Query(value = "SELECT * FROM mingju WHERE id>=(SELECT FLOOR(RAND() * (SELECT MAX(id) FROM mingju)))  ORDER BY id LIMIT 20", nativeQuery=true)
    List<Mingju> getRandMingju();

    @Query("select m from Mingju m ")
    Page<Mingju> getMingjuList(Pageable pageable);

    @Query("select m from Mingju m where m.theme=:theme")
    Page<Mingju> searchByTheme(@Param("theme") String theme, Pageable pageable);

    Optional<Mingju> findByCode(String code);
}
